#!/usr/bin/perl

use CGI;
use Encode;
use URI::Escape;


my $query = CGI->new;
#my $query = "http://www.imdb.com/search/title?title=test1&title_type=test2";
#my $title = $query->param('title');
#my $title_type = $query->param('title_type');
my $url = "";

my $qs = uri_unescape($ENV{ "QUERY_STRING" });
%queryString = ();
@parts = split(/\&/, $qs);
foreach $part (@parts) {
( $name, $value ) = split( /\=/, $part );
$queryString{ "$name" } = $value;
}

#$queryString{"title"} = 'the%20godfather';
#$queryString{"type"} = 'feature';


#my $title = 'the godfather';
#my $title_type = 'feature';

if ($title_type eq "all"){
	$url="http://www.imdb.com/search/title?title=" . $queryString{"title"};
}
else{
	$url="http://www.imdb.com/search/title?title=" . $queryString{"title"} . "&title_type=" . $queryString{"title_type"}; 
}

my $hash = [];

#output XML
print "Content-type: text/xml\n\n";

print "<rsp stat=\"ok\">";



use LWP::Simple;

my $content_url = get $url;

$content_url =~ s/\n//g;


if ($content_url =~ m/<table class=\"results\">(.*?)<\/table>/) {
my $content = $1;



my $row_count = 1;

if ($content =~ m/<td class=\"number\">1\.(.*?)<\/tr>/){
	processRow($row_count, $1);
}

if ($content =~ m/<td class=\"number\">2\.(.*?)<\/tr>/){
	$row_count = 2;
	processRow($row_count, $1);
}

if ($content =~ m/<td class=\"number\">3\.(.*?)<\/tr>/){
	$row_count = 3;
	processRow($row_count, $1);
}

if ($content =~ m/<td class=\"number\">4\.(.*?)<\/tr>/){
	$row_count = 4;
	processRow($row_count, $1);
}
if ($content =~ m/<td class=\"number\">5\.(.*?)<\/tr>/){
	$row_count = 5;
	processRow($row_count, $1);
}

print "<results total=\"$row_count\">";


#XML data
for ($k = 1; $k<$row_count+1; $k++){
print "<result ";
for ($i=0; $i<6; $i++){
	print $hash[$k][$i];
}
print "/>";
}


} 

#HOW DO I HANDLE NO RESULTS?
else {

print "<results total=\"0\">";
}

print "</results>";
print "</rsp>";

sub processRow() {

($count, $content) = @_;

	#The exact title of the movie - <a href="/title/ ------------ </a>
	if ($content =~ m/<a href=\"\/title\/(.*?)\/\">(.*?)<\/a>/){
	my $movie_title = $2;
	$hash[$count][1] = 'title="' . $movie_title . '" '; 
	}

	#The release year - <span class="year_type">----------- </span>
	if ($content =~ m/<span class=\"year_type\">\((.*?)[ |\)]/){
	my $year = $1; 
	$hash[$count][2] = 'year="' . $year . '" ';
	}

	#The movie poster (small icon)  <td class="image"> grab src of img tag </td>
	if ($content =~ m/<td class=\"image\">.*?<img src=\"(.*?)\"/){
	my $image = $1; 
	$hash[$count][0] = 'cover="' . $image . '" '; 
	}

	#The rating (number of stars) - <span class="rating-rating"> grab span with class value </span>
	if ($content =~ m/<span class=\"rating-rating\"><span class=\"value\">(.*?)<\/span>/){
	my $rating = $1;  
	$hash[$count][4] = 'rating="' . $rating . '" ';
	}
	else{
	$hash[$count][4] = 'rating="NA" ';  
	}
  

	#The director name(s) - <span class="credit"> grab first anchor's value <span>
	if ($content =~ m/<span class=\"credit\">.*?Dir:(.*?)With:/){
   @directors = split(',', $1);
   my $d_cnt = 0;
	my @d_result = '';
   foreach my $val (@directors) {
           $val =~ m/<a.*>(.*?)<\/a>/;
     @d_result[$d_cnt++] = decode_utf8($1);
   }
        $hash[$count][3] = 'director="' . join(', ', @d_result) . '" ';
        }
	else{
	$hash[$count][3] = 'director="NA" ';  
	}

	#The link to the specific movie - get href from anchor tag from #1
	if ($content =~ m/<td class=\"title\">.*?<a href=\"\/title\/(.*?)\">/){
	my $link = 'http://imdb.com/title/' . $1;
 	$hash[$count][5] = 'details="' . $link . '"';

    }
  
}
